package com.example.assignment7;

import com.example.assignment7.aspect.StopWatchAspect;
import com.example.assignment7.controller.AspectController;
import com.sun.istack.internal.logging.Logger;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;

import static org.assertj.core.api.Assertions.assertThat;


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class testControllerAspect {

    @LocalServerPort
    private int port;

    @Autowired
    StopWatchAspect stopWatchAspect;

    @Autowired
    private TestRestTemplate restTemplate;

    private static final Logger logger = Logger.getLogger(StopWatchAspect.class);

    @Test
    public void testAspect(){
        assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/hello",void.class));
        assertThat(this.restTemplate.postForObject("http://localhost:" + port + "/hello",null,void.class));
        assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/hello",void.class));
        stopWatchAspect.getUseTime().forEach((k,v)->{
            logger.info("方法:"+k+";内容:"+v);
        });
        stopWatchAspect.getMetrics().forEach((k,v)->{
            logger.info("方法"+k+";时间:"+v);
        });
        stopWatchAspect.getExmetrics().forEach((k,v)->{
            logger.info("方法"+k+";时间:"+v);
        });
    }
}
